<?php
namespace app\common\logic;

use PHPMailer\PHPMailer\PHPMailer;

class Email extends Base {

    /**
     * @des 发送邮件
     * @param $recipient 接收者邮箱
     * @param string $subject 主题
     * @param string $content 内容
     * @throws \PHPMailer\PHPMailer\Exception
     */
    public  function  sendMail($recipient, $subject = '', $content = ''){
        $senderInfo = [
            'host'     => 'smtp.gmail.com',
            'account'  => 'hawkeyeplatform1@gmail.com',
            'password' => 'S8RPl0yI47JopPIz2yUNpW2u',
            'port'     => '587',
            'username' => 'hawkeye'
        ];
        $mail = new PHPMailer;
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = $senderInfo['host'];  // 邮件服务器
        $mail->SMTPAuth = true;                            // Enable SMTP authentication
        $mail->Username = $senderInfo['account'];    // 邮箱账号
        $mail->Password = $senderInfo['password'];   // 邮箱密码
        $mail->SMTPSecure = 'tls';                          // Enable TLS encryption, `ssl` also accepted
        $mail->Port = $senderInfo['port'];           // 端口号
        $mail->Timeout = 600;
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->setFrom($senderInfo['account'], $senderInfo['username']);
        $mail->addAddress($recipient);
        $mail->isHTML(true);
        $mail->CharSet = 'UTF-8';
        $mail->Subject = $subject;
        $mail->Body = $content;
        $mail->AltBody = '';
        if (!$mail->send()) {
            $this->message = $mail->ErrorInfo;
            return false;
        } else {
            return true;
        }

    }
}